using Digitalmes.Application.Mes.Equipments.Inspections;
using Digitalmes.Application.Mes.Equipments.Inspections.Commands;
using Digitalmes.Application.Mes.Equipments.Inspections.Queries;

namespace Digitalmes.WebApi.Endpoints.Mes.Equipments;

/// <summary>
/// 设备检点计划终结点。
/// </summary>
public sealed class InspectionPlanEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "mes/equipment/inspection", "plan")
            .RequireAuthorization()
            .MapGet(GetMachineryInspectionPlanPagedListAsync, "list-page")
            .MapPost(CreateMachineryInspectionPlanAsync)
            .MapPut(UpdateMachineryInspectionPlanAsync, "{id}")
            .MapDelete(DeleteMachineryInspectionPlanAsync, "{id}");
    }

    [EndpointDescription("分页查询设备点检计划")]
    public async Task<IApiResult> GetMachineryInspectionPlanPagedListAsync(ISender sender, [AsParameters] GetInspectionPlanListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建设备点检计划")]
    public async Task<IApiResult> CreateMachineryInspectionPlanAsync(ISender sender, EquipInspectionPlanDto dto)
    {
        var result = await sender.Send(new CreateInspectionPlanCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新设备点检计划")]
    public async Task<IApiResult> UpdateMachineryInspectionPlanAsync(ISender sender, long id, EquipInspectionPlanDto dto)
    {
        var result = await sender.Send(new UpdateInspectionPlanCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除设备点检计划")]
    public async Task<IApiResult> DeleteMachineryInspectionPlanAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteInspectionPlanCommand(id));
        return result.ToApiResult();
    }
}
